<?php

namespace App\Models;

use App\User;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class CarouselPicCategory extends Model
{
    protected $table = 'c_carousel_pic_categories';

    /***
     * @param $filter
     * @return array
     * 获取轮播图
     */
    public function getImages($filter)
    {
        $res = $this->where([['carousel_pic_id', '=', $filter['cate_id']]])->get();
        $host = new Url();
        foreach ($res as $k => $v) {
            $v->pic = $host::getHttpHost() . 'upload/' . $v->pic;
        }
        $nums = 0;
        if (isset($filter['api_token'])) {
            $user = (new User())->where([['api_token', '=', $filter['api_token']]])->first();

            if ($user) {
                $nums = (new VisitUser())->where([['user_id', '=', $user->id], ['is_read', '=', 1]])->count();
            }
        }
        if (!empty($res)) {
            $list = $res->toArray();
            $list[0]['read'] = $nums > 0 ? 1 : 0;
            return $list;
        }
        return [];
    }
}
